package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.api.model.vo.CartItemForPreOrderVo;
import com.weixing.mall.provider.api.model.vo.ItemVo;
import com.weixing.mall.provider.mapper.ProductMapper;
import com.weixing.mall.provider.mapper.ShopCartMapper;
import com.weixing.mall.provider.model.domain.Product;
import com.weixing.mall.provider.model.domain.ShopCart;
import com.weixing.mall.provider.model.dto.CartChgNumDto;
import com.weixing.mall.provider.model.dto.ShopCartDto;
import com.weixing.mall.provider.model.enums.CartStatusEnum;
import com.weixing.mall.provider.model.vo.ShopCartVo;
import com.weixing.mall.provider.service.IShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author wx
 * @Date 2019/9/24 14:01
 * @Version 1.0
 */

/**
 * <p>
 * 商品模块-购物车 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-20
 */
@Service
public class ShopCartServiceImpl extends ServiceImpl<ShopCartMapper, ShopCart> implements IShopCartService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void save(ShopCartDto shopCartDto, Long mid) {
        ShopCart shopCart = new ShopCart();
        BeanUtils.copyProperties(shopCartDto, shopCart);
        Product product = productMapper.selectById(shopCartDto.getProductId());
        System.out.println(product + "，" + shopCartDto);
        shopCart.setPrice(product.getDiscountPrice());
        shopCart.setMemberId(mid);
        save(shopCart);
    }

    @Override
    public void changeNum(CartChgNumDto shopCartEditQuantityDto) {
        baseMapper.changeNum(shopCartEditQuantityDto);
    }


    /**
     * 方法描述: 查询购物车列表
     * @Author Small
     * @param memberId
     * @Date 2020/1/12 15:14
     * @return java.util.List<com.weixing.mall.provider.model.vo.ShopCartVo>
     */
    @Override
    public List<ShopCartVo> selectCartListByMemberId(Long memberId) {
        return baseMapper.selectByMemberId(memberId);
    }

    /**
     * 方法描述: 选中商品
     * @Author Small
     * @param itenId
     * @Date 2020/1/12 15:04
     * @return void
     */
    @Override
    public void selectItem(Long itenId) {
        ShopCart byId = getById(itenId);
        ShopCart shopCart = new ShopCart();
        shopCart.setId(byId.getId());
        shopCart.setStatus(CartStatusEnum.SELECTED.getValue());
        updateById(shopCart);
    }

    /**
     * 方法描述: 取消选中商品
     * @Author Small
     * @param itenId
     * @Date 2020/1/12 15:04
     * @return void
     */
    @Override
    public void cancelItem(Long itenId) {
        ShopCart byId = getById(itenId);
        ShopCart shopCart = new ShopCart();
        shopCart.setId(byId.getId());
        shopCart.setStatus(CartStatusEnum.NOT_SELECTED.getValue());
        updateById(shopCart);
    }

    @Override
    public void selectAllItem(long currentUserId) {
        UpdateWrapper<ShopCart> updateWrapper = new UpdateWrapper<ShopCart>().set("status", CartStatusEnum.SELECTED.getValue()).eq("member_id", currentUserId);
        update(updateWrapper);
    }

    @Override
    public void cancelAllItem(long currentUserId) {
        UpdateWrapper<ShopCart> updateWrapper = new UpdateWrapper<ShopCart>().set("status", CartStatusEnum.NOT_SELECTED.getValue()).eq("member_id", currentUserId);
        update(updateWrapper);
    }

    @Override
    public List<ItemVo> loadSelectedItem(Long memberId) {
        List<ItemVo> vos = baseMapper.loadSelectedItem(memberId);
        return vos;
    }


}
